<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Mail;

class UsersController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth',[
           'except' => ['show' , 'create' , 'store' , 'index' , 'confirmEmail']
        ]);
    }

    public function index()
    {
        $users=User::paginate(10);
        return  view('users.index',compact('users'));
    }

    public function create()
    {
        return view('users.create');
    }

    public function show(User $user)
    {
//            $statuses=$user->statuses()->get();
//            var_dump($statuses);
//            die();
        $statuses = $user->statuses()
            ->orderBy('created_at', 'desc')
            ->paginate(30);
        return view('users.show', compact('user', 'statuses'));


    }

    public function store(Request $request)
    {
        $this->validate($request,[
            'name' => 'required|max:50',
            'email' => 'required|email|unique:users|max:255',
            'password' => 'required|confirmed|min:3'
        ]);
        $user=User::create([
           'name' => $request->name,
           'email' => $request->email,
           'password' => bcrypt($request->password)
        ]);
        $this->sendEmailConfirmationTo($user);
        session()->flash('success', '验证邮件已发送到你的注册邮箱上，请注意查收。');
        return redirect('/');
    }

    protected function sendEmailConfirmationTo($user)
    {
        $view = 'emails.confirm';
        $data = compact('user');
        $from = '1@qq.com';
        $name = '1';
        $to = $user->email;
        $subject = "感谢注册 Sample 应用！请确认你的邮箱。";

        Mail::send($view, $data, function ($message) use ($from, $name, $to, $subject) {
            $message->from($from, $name)->to($to)->subject($subject);
        });
    }

    public function confirmEmail($token)
    {
        $user=User::where('activation_token',$token)->firstOrfail();

        $user->activated = true;
        $user->activation_token = null;
        $user->save();

        Auth::login($user);
        session()->flash('success', '恭喜你，激活成功！');
        return redirect()->route('users.show', [$user]);
    }

    public function edit(User $user)
    {
        $this->authorize('update',$user);
        return view('users.edit',compact('user'));
    }

    public function update(Request $request)
    {
        $user=Auth::user();
        $this->authorize('update',$user);
        $this->validate($request,[
            'name' => 'required|max:50',
        ]);
        $data=[];
        $data['name']=$request->name;
        if($request->password) {
            $user->update([
                'name' => $request->name,
                'password' => bcrypt($request->password)
            ]);
        }else{
            $user->update($data);
        }
        session()->flash('success','更新资料成功！');

        return redirect()->route('users.show',[$user]);
    }

    public function destroy(User $user)
    {
        $this->authorize('destroy', $user);
        $user->delete();
        session()->flash('success', '删除成功！');
        return redirect()->back();
    }

    public function followings(User $user)
    {
        $users=$user->followings()->paginate(30);
        $title='关注的人';
        return view('users.show_follow',compact('users','title'));
    }

    public function followers(User $user)
    {
        $users=$user->followers()->paginate(30);
        $title='粉丝';
        return view('users.show_follow',compact('users','title'));
    }

    public function nima(User $user)
    {
//        $dd=$user->statuses();
//        var_dump($dd);
//        die();
        $dd=$user->statuses;
        var_dump($dd);
        die();
    }
}
